package org.lagou.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lagou.Dao.CompanyDao;
import org.lagou.Dao.GetResumeDao;
import org.lagou.Dao.ResumeDao;
import org.lagou.Dao.UserDao;
import org.lagou.Dao.impl.CompanyDaoImpl;
import org.lagou.Dao.impl.GetResumeDaoImpl;
import org.lagou.Dao.impl.ResumeDaoImpl;
import org.lagou.Dao.impl.UserDaoImpl;
import org.lagou.bean.CompanyBean;
import org.lagou.bean.GetResumeBean;
import org.lagou.bean.ReponseBody;
import org.lagou.bean.ResumeBean;
import org.lagou.bean.UserBean;
import org.lagou.tools.Tools;

import com.google.gson.Gson;

@WebServlet("/GetResumeServlet")
public class GetResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	GetResumeDao dao = new GetResumeDaoImpl();
	Gson gson = new Gson();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String type = request.getParameter("type");

		switch (type) {
		case "getGetResumeAll":
			getGetResumeAll(request, response);
			break;
		case "getGetResumePagin":
			getGetResumePagin(request, response);
			break;
		case "getGetResumeCount":
			getGetResumeCount(request, response);
			break;
		case "addGetResume":
			addGetResume(request, response);
			break;
		case "updateGetResume":
			updateGetResume(request, response);
			break;
		case "deleteGetResume":
			deleteGetResume(request, response);
			break;
		case "getGetResumeByCompanyid":
			getGetResumeByCompanyid(request, response);
			break;
		case "updatestate":
			updatestate(request, response);
			break;
		case "updatestatee":
			updatestatee(request, response);
			break;
		case "getGetResumeByResumeid":
			getGetResumeByResumeid(request, response);
			break;
		case "getGetResumeByState":
			getGetResumeByState(request, response);
			break;
		default:
			break;
		}
	}

	private void getGetResumeByState(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		List<UserBean> ulist = udao.getByEmailUser(email);
		int userid = 0;
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		int state = Integer.parseInt(request.getParameter("state"));

		rb.setData(dao.getGetResumeByState(userid,state));

		try {
			response.getWriter().write(new Gson().toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updatestatee(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		int id = Integer.parseInt(request.getParameter("id"));

		rb.setData(dao.updatestatee(id));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getGetResumeByResumeid(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		List<UserBean> ulist = udao.getByEmailUser(email);
		int userid = 0;
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		ResumeDao rdao = new ResumeDaoImpl();
		List<ResumeBean> rlist = rdao.getResumeByUserid(userid);
		int resumeid = -1;
		for (ResumeBean r : rlist) {
			resumeid = r.getId();
		}

		if (!dao.getGetResumeByResumeid(resumeid).isEmpty()) {
			rb.setMsg("查询成功");
			rb.setData(dao.getGetResumeByResumeid(resumeid));
		} else {
			rb.setMsg("查询失败");
			rb.setStatus(0);
		}

		try {
			response.getWriter().write(new Gson().toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updatestate(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		int id = Integer.parseInt(request.getParameter("id"));

		rb.setData(dao.updatestate(id));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void getGetResumeByCompanyid(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		List<UserBean> ulist = udao.getByEmailUser(email);
		int userid = 0;
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		CompanyDao cdao = new CompanyDaoImpl();
		List<CompanyBean> clist = cdao.getCompanyByUserid(userid);
		int companyid = -1;
		for (CompanyBean c : clist) {
			companyid = c.getId();
		}

		if (!dao.getGetResumeByCompanyid(companyid).isEmpty()) {
			rb.setMsg("查询成功");
			rb.setData(dao.getGetResumeByCompanyid(companyid));
		} else {
			rb.setMsg("查询失败");
			rb.setStatus(0);
		}

		try {
			response.getWriter().write(new Gson().toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getGetResumeAll(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		rb.setData(dao.getGetResumeAll());
		try {
			response.getWriter().write(new Gson().toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getGetResumePagin(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		int pageIndex = request.getParameter("pageIndex") == null ? 1
				: Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = request.getParameter("pageSize") == null ? 1
				: Integer.parseInt(request.getParameter("pageSize"));
		rb.setData(dao.getGetResumePagin(pageIndex, pageSize));
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getGetResumeCount(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		rb.setData(dao.getGetResumeCount());
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void updateGetResume(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();

		int id = Integer.parseInt(request.getParameter("id"));
		int companyid = Integer.parseInt(request.getParameter("companyid"));
		int positionid = Integer.parseInt(request.getParameter("positionid"));
		int resumeid = Integer.parseInt(request.getParameter("resumeid"));
		int state = Integer.parseInt(request.getParameter("state"));
		String companyname = request.getParameter("companyname");
		String positionname = request.getParameter("positionname");
		int userid = Integer.parseInt(request.getParameter("userid"));
		String date = Tools.getNowDate();

		GetResumeBean info = new GetResumeBean(id, companyid, positionid, resumeid, state, companyname, positionname,
				userid, date);
		rb.setData(dao.updateGetResume(info));

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void addGetResume(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb = new ReponseBody();
		int id = 0;
		int companyid = Integer.parseInt(request.getParameter("companyid"));
		int positionid = Integer.parseInt(request.getParameter("positionid"));
		int resumeid = Integer.parseInt(request.getParameter("resumeid"));
		int state = 1;
		String companyname = request.getParameter("companyname");
		String positionname = request.getParameter("positionname");
		// 读取session值
		String email = (String) request.getSession().getAttribute("email");
		UserDao udao = new UserDaoImpl();
		List<UserBean> ulist = udao.getByEmailUser(email);
		int userid = 0;
		for (UserBean u : ulist) {
			userid = u.getId();
		}
		String date = Tools.getNowDate();

		GetResumeBean info = new GetResumeBean(id, companyid, positionid, resumeid, state, companyname, positionname,
				userid, date);

		if (dao.addGetResume(info) > 0) {
			rb.setMsg("投递成功");
		} else {
			rb.setMsg("投递失败");
			rb.setStatus(0);
		}

		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void deleteGetResume(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ReponseBody rb = new ReponseBody();

		int id = Integer.parseInt(request.getParameter("id"));
		rb.setData(dao.deleteGetResume(id));
		response.getWriter().write(new Gson().toJson(rb));
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
